package com.gzdh.device.pay.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.device.comm.vo.Result;
import com.gzdh.device.task.entity.TaskCenter;
import com.gzdh.device.task.service.TaskCenterService;
import com.gzdh.device.wx.entity.WxSubMchid;
import com.gzdh.device.wx.service.WxSubMchidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/pay/leshua/v1")
public class PayLeshuaController {

    @Autowired
    WxSubMchidService wxSubMchidService;


    @Autowired
    TaskCenterService taskCenterService;

    @GetMapping("getLeshuaAccount")
    public Result<Object> getLeshuaAccount() {
        Result<Object> result = new Result<>();
        List<WxSubMchid> wxSubMchidList = wxSubMchidService.list();
        result.setResult(wxSubMchidList);
        return result;
    }

    @GetMapping("getLeshuaAccountById")
    public Result<Object> getLeshuaAccount(@RequestParam("id") String id) {
        Result<Object> result = new Result<>();
        WxSubMchid wxSubMchid = wxSubMchidService.getById(id);
        result.setResult(wxSubMchid);
        return result;
    }

    @PostMapping("addLeshuaAccount")
    public Result<Object> addLeshuaAccount(@RequestBody JSONObject jsonObject) {
        Result<Object> result = new Result<>();
        String subMchId = jsonObject.getString("subMchid");
        String name = jsonObject.getString("name");
        String merchant = jsonObject.getString("merchant");
        WxSubMchid wxSubMchid = new WxSubMchid();
        int counts = wxSubMchidService.count(new QueryWrapper<WxSubMchid>().lambda().eq(WxSubMchid::getMerchant, merchant)
                .or().eq(WxSubMchid::getName, name).or().eq(WxSubMchid::getSubMchid, subMchId));
        if (counts > 0) {
            return Result.error("数据存在重复，请核对后提交");
        }
        wxSubMchid.setMerchant(merchant);
        wxSubMchid.setName(name);
        wxSubMchid.setSubMchid(subMchId);
        wxSubMchidService.save(wxSubMchid);
        return result;
    }

    @PostMapping("updateSyncBill")
    public Result<Object> updateSyncBill(@RequestBody JSONObject jsonObject) {
        Result<Object> result = new Result<>();
        String id = jsonObject.getString("id");
        boolean syncBill = jsonObject.getBoolean("syncBill");
        WxSubMchid wxSubMchid = wxSubMchidService.getById(id);
        if (wxSubMchid == null) {
            return Result.error("记录不存在");
        }
        if (!wxSubMchid.getInit()) {
            return Result.error("账号尚未初始化，请初始化后再操作");
        }
        wxSubMchidService.lambdaUpdate().set(WxSubMchid::getSyncBill, syncBill).eq(WxSubMchid::getId, id).update();
        return result;
    }

    /**
     * 初始化账号
     *
     * @return
     */
    @GetMapping("initAccount")
    public Result<Object> initAccount(@RequestParam("id") String id) {
        Result<Object> result = new Result<>();
        //查询账号是由有任务
        int counts = taskCenterService.count(new QueryWrapper<TaskCenter>().lambda().eq(TaskCenter::getCondition, id)
                .eq(TaskCenter::getType, "1011").eq(TaskCenter::getStatus, "0").or().eq(TaskCenter::getStatus, "1"));
        if (counts > 0) {
            return Result.error("当前账号已经初始化中，请稍后核查");
        }
        TaskCenter taskCenter = new TaskCenter();
        taskCenter.setType("1011");
        taskCenter.setTitle("支付账号初始化");
        taskCenter.setCondition(id);
        taskCenter.setDescs("");
        taskCenter.setParameter(id);        taskCenterService.save(taskCenter);
        return result;
    }


}
